//
//  DFMainViewController.swift
//  新浪微博
//
//  Created by dafei on 16/5/19.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

class DFMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
      addChildViewControllers()
        
        let mainTabbar = DFMainTabbar()
        mainTabbar.plusBtn.addTarget(self, action: #selector(DFMainViewController.plusBtnDidClick), forControlEvents: .TouchUpInside)
        
        //赋值  只读属性不能够直接赋值 但是通过kvc 间接赋值
        print(tabBar.classForCoder)
        // setObject: forley == > 字典
        setValue(mainTabbar, forKey: "tabBar")
//        print(tabBar.classForCoder)
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
    }
    
    //为了让外界不能够访问按钮的监听方法  需要添加private
    //一旦添加了private 这个时候这个 就具备动态派发的特性
    //需要添加@objc 告诉系统的这个方法 是基于oc
    
    @objc private func plusBtnDidClick() {
        //添加撰写的选择视图
        let composeView = DFCompseView()
        composeView.show(targetVC: self)
//        
//        let window = UIApplication.sharedApplication().keyWindow
//        window?.addSubview(composeView)
        
        print("被点击了")
    
    }
    
    
    private func addChildViewControllers() {
        
        addChildViewController(DFHomeViewController(), title: "首页", imageNamed: "tabbar_home")
        addChildViewController(DFDiscoverTableViewController(), title: "发现", imageNamed: "tabbar_discover")
        addChildViewController(DFMessageTableViewController(), title: "消息", imageNamed: "tabbar_message_center")
        addChildViewController(DFProfileTableViewController(), title: "我的", imageNamed: "tabbar_profile")
        
        
    }
 
    private  func addChildViewController(vc:UITableViewController , title: String, imageNamed: String){
        
        //设置vc的导航视图控制器的导航条的模型对象tilte
//        vc.navigationItem.title = "HHAH"
        
        
        //标签和导航条一致的用
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageNamed)
        vc.tabBarItem.selectedImage = UIImage(named: imageNamed + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        
        //设置title颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
        //统一设置title的颜色
//        tabBar.tintColor = UIColor.orangeColor()
        //向上移动
        vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -3)
        
        //内嵌到导航视图控制器中
        let nav = DFBaseNavController(rootViewController: vc)
        
        //添加子视图控制器中
        addChildViewController(nav)
    
  
    
    }
    

}
